import { getClipboardData } from '@/plugins/tui-clipboard/tui-clipboard.js';
import { PagesUrl } from '@/config/pagesUrl/index.js';
import { allSettled, padStart, getImageFromGoods, getImagesFromOrder, dateFormat, getProperty } from '@/common/utils/utils.js';
import store from '@/config/store';
import { mediaFilter } from '@/common/utils/utils.js';

let myGlobal = {};
// 复制
myGlobal.getClipboardData = getClipboardData;

// 页面跳转
myGlobal.onPageJump = function(urlKey, params, mode="navigateTo") { 
	console.log('页面跳转 urlKey:', urlKey)
	let url = PagesUrl[urlKey];
	if (!url) {
		myGlobal.toast('功能即将上线，敬请期待')
		return;
	}
	if (url.tips) {
		myGlobal.toast(url.tips);
		return;
	}
	if (typeof url === 'object') {
		const check = auth.checkScope(url.menu_id);
		if (check) {
			url = url.url
		} else {
			return;
		}
	}
	if (params && typeof params === 'object') {
		let paramString = Object.keys(params).map(key => key+'='+params[key]).join('&');
		url += (url.indexOf('?') > -1 ? '&' : '?') + paramString;
	}
	if (mode === 'redirectTo') {
		uni.redirectTo({ url: url }) 
	} else {
		uni.navigateTo({ url: url }) 
	}
};

// 拨号
myGlobal.makePhoneCall = function(phone){
	uni.showModal({
		title: '拨打 '+phone,
		success(res) {
			if (res.confirm) {
				uni.makePhoneCall({ phoneNumber: phone }) 
			}
		}
	})
};

// 图片预览
myGlobal.previewImage = function(url, urls) {
	if (!url) return;
	url = mediaFilter(url);
	if (urls && urls.length > 0) {
		urls = urls.map(e => mediaFilter(e));
	}
	uni.previewImage({ 
		current: url, 
		urls: urls ? urls : [url],
		loop: true
	}) 
};

// 预览视频
myGlobal.previewVideo = function(url) {
	if (!url) return;
	url = mediaFilter(url);
	uni.$emit('playVideo', url);
};

// 视频图片混合预览
myGlobal.previewMixImage = function(index, list) {
	function filter(e) {
		return e.type === 'video' ? 
				mediaFilter(e.value, 'video') :
				mediaFilter(e.value)
	}
	let urls = list.map(e => filter(e));
	let url = urls[index]
	uni.previewImage({ 
		current: url, 
		urls: urls,
	}) 
};

// 图片视频预处理
myGlobal.getMediaUrl = mediaFilter;

// 无图标提示
myGlobal.toast = function(options, callback) {
	if (!options) return;
	let title = '',
		duration = 4000;
	if (typeof options === 'string') {
		title = options;
	} else {
		title = options.title;
		duration = options.duration || duration;
	}
	uni.showToast({
		title: title,
		duration: duration,
		icon: 'none'
	})
	if (callback) {
		setTimeout(() => {
			callback()
		}, duration)
	}
};

// 成功提示
myGlobal.success = function(options, callback) {
	if (!options) return;
	let title = '',
		duration = 800;
	if (typeof options === 'string') {
		title = options;
	} else {
		title = options.title;
		duration = options.duration || duration;
	}
	uni.showToast({
		title: title,
		duration: duration,
		icon: 'success'
	})
	if (callback) {
		setTimeout(() => {
			callback()
		}, duration)
	}
};

// 扫码
myGlobal.scanCode = function() {
	return new Promise((resolve, reject) => {
		uni.scanCode({
			success: function (res) {
				resolve(res.result)
			},
			fail: function (err) {
				reject(err)
			}
		})
	})
};

// 添加单位
myGlobal.addUnit = function(value = 'auto', unit = 'rpx') {
	value = String(value);
	return /^-?\d+(\.\d+)?$/.test(value) ? value + unit : value;
};

myGlobal.install = function (Vue, options) {
	Vue.prototype.$g = myGlobal;

	if (process.env.NODE_ENV !== 'development') {
		console.log = () => {}
	}
	
	// 添加padStart方法
	if (!String.prototype.padStart) {
	    String.prototype.padStart = padStart
	}
	
	// 添加format方法
	if (!Date.prototype.format) {
	    Date.prototype.format = dateFormat
	}
	
	if (!Promise.allSettled) {
		Promise.allSettled = allSettled
	}
}

export {
	myGlobal
}